//
//  JCreateActivityController.m
//  HeadIndustry
//
//  Created by jun on 2017/9/21.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JCreateActivityController.h"
#import "JSelImgView.h"
#import <QBImagePickerController/QBImagePickerController.h>
#import "NSDate+XHExtension.h"
#import "XHDatePickerView.h"



@interface JCreateActivityController ()<QBImagePickerControllerDelegate ,UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) QBImagePickerController *imagePickerController;

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITextField *tfName ;
@property (nonatomic, strong) UITextField *peopleNum ;
@property (nonatomic, strong) UITextField *userAddress ;
@property (nonatomic, strong) UITextView *contentText ;

@property (nonatomic, strong) YYLabel *startLeftTime;
@property (nonatomic, strong) YYLabel *startRightTime;
@property (nonatomic, strong) YYLabel *stopLeftTime;
@property (nonatomic, strong) YYLabel *stopRightTime;
@property (nonatomic, strong) UITextField *userLocation;

@property (nonatomic, strong) UIFont *textFont;
@property (nonatomic, strong) UIColor *titleColor;
@property (nonatomic, strong) UIColor *grayColor;

@property (nonatomic, strong) NSMutableArray *selImgArr;
@property (nonatomic, strong) NSMutableArray *selImgBtnArr;
@property (nonatomic, assign) NSInteger  selBtnTag;
@property (nonatomic, assign) NSInteger  selImgNum;
@property (nonatomic, assign) CGFloat  minY;
@property (nonatomic, strong) UIButton *submitBtn ;
@property (nonatomic, strong) NSMutableDictionary *paramerDic ;
@property (nonatomic, copy) NSString *peopleStr ;

@end

@implementation JCreateActivityController


- (NSMutableDictionary *)paramerDic
{
    if (_paramerDic == nil) {
        _paramerDic = [NSMutableDictionary dictionary];
        
    }
    return _paramerDic;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [super InitUI];
    self.titleLabel.text = @"创建活动";
    self.leftButton.hidden = NO;
    [self.leftButton setImage:[UIImage imageNamed:backBtnImg] forState:UIControlStateNormal];
    self.leftButton.frame = CGRectMake(8, self.leftButton.frame.origin.y, 30, 30);
    _selImgBtnArr = [NSMutableArray array];
    _selImgArr = [NSMutableArray array];
    _textFont = kFONT_TITLE(16);
    _titleColor = kCOLOR_HEX(0x646464);
    _grayColor = kRGBColor(210, 210, 206);
    
    [self setConfigBgView];
    
    [self selTimeClick];
}

- (void)setConfigBgView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWindowH, self.contentView.height)];
    _scrollView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_scrollView];
    
    NSArray *nameArr = @[@"活动主题:",@"开始时间:",@"结束时间:",@"人数限制:",@"省市区:",@"详细地址:",@"活动内容:"];
    
    for (int i = 0; i < nameArr.count ; i++) {
        YYLabel *title1 = [YYLabel new];
        title1.top = i * 50;
        title1.left = 10;
        title1.size = CGSizeMake(75, 50);
        title1.font = kFONT_TITLE(16);
        title1.textColor = _titleColor;
        title1.textAlignment = NSTextAlignmentRight;
        [_scrollView addSubview:title1];
        title1.text = nameArr[i];
    }
    
    UITextField *name = [[UITextField alloc] initWithFrame:CGRectMake(90, 0, kWindowW - 90 - 10, 50)];
    name.font = _textFont;
    [_scrollView addSubview:name];
    _tfName = name;
    
    YYLabel *title1 = [YYLabel new];
    title1.top = name.bottom;
    title1.left = name.left;
    title1.size = CGSizeMake((name.width)/2.0, 50);
    title1.font = kFONT_TITLE(16);
    title1.textColor = _grayColor;
    title1.textAlignment = NSTextAlignmentLeft;
    [_scrollView addSubview:title1];
    title1.text = @"年-月-日";
    _startLeftTime = title1;
    
    YYLabel *title2 = [YYLabel new];
    title2.top = name.bottom;
    title2.left = title1.right;
    title2.size = CGSizeMake((name.width)/2.0, 50);
    title2.font = kFONT_TITLE(16);
//    title1.textColor = ;
    title2.textAlignment = NSTextAlignmentLeft;
    [_scrollView addSubview:title2];
    title2.text = @"00:00";
    _startRightTime = title2;
    
    YYLabel *title11 = [YYLabel new];
    title11.top = title1.bottom;
    title11.left = title1.left;
    title11.size = CGSizeMake(title1.width, 50);
    title11.font = kFONT_TITLE(16);
    title11.textColor = _grayColor;
    title11.textAlignment = NSTextAlignmentLeft;
    [_scrollView addSubview:title11];
    title11.text = @"年-月-日";
    _stopLeftTime = title11;
    
    YYLabel *title12 = [YYLabel new];
    title12.top = title2.bottom;
    title12.left = title11.right;
    title12.size = CGSizeMake(title2.width, 50);
    title12.font = kFONT_TITLE(16);
//    title12.textColor = _grayColor;
    title12.textAlignment = NSTextAlignmentLeft;
    [_scrollView addSubview:title12];
    title12.text = @"00:00";
    _stopRightTime = title12;
    
    UITextField *peopleNum = [[UITextField alloc] initWithFrame:CGRectMake(title11.left, title11.bottom, name.width, 50)];
    peopleNum.font = _textFont;
    [_scrollView addSubview:peopleNum];
    peopleNum.keyboardType = UIKeyboardTypeNumberPad;
    [peopleNum addTarget:self action:@selector(peopleNumTextField:) forControlEvents:UIControlEventEditingChanged];
    _peopleNum = peopleNum;
    
    UITextField *location = [UITextField new];
    location.top = peopleNum.bottom;
    location.left = peopleNum.left;
    location.size = CGSizeMake(peopleNum.width - 70, 50);
    location.font = kFONT_TITLE(16);
    //    title12.textColor = _grayColor;
    location.textAlignment = NSTextAlignmentLeft;
    [_scrollView addSubview:location];
    _userLocation = location;
    
//    UITextField *location = [[UITextField alloc] initWithFrame:CGRectMake(title11.left, title11.bottom, name.width, 50)];
//    peopleNum.font = _textFont;
//    [_scrollView addSubview:peopleNum];
//    peopleNum.keyboardType = UIKeyboardTypeNumberPad;
//    [peopleNum addTarget:self action:@selector(peopleNumTextField:) forControlEvents:UIControlEventValueChanged];
//    _peopleNum = peopleNum;
    
    
    
    UIButton *locationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    locationBtn.frame = CGRectMake(location.right, location.top, 70, 50);
    [locationBtn setImage:[UIImage imageNamed:@"dingwei"] forState:UIControlStateNormal];
    [locationBtn setTitle:@"定位" forState:UIControlStateNormal];
    [locationBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [locationBtn addTarget:self action:@selector(locationBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:locationBtn];
    
    UITextField *userAddress = [[UITextField alloc] initWithFrame:CGRectMake(location.left, location.bottom, name.width, 50)];
    userAddress.font = _textFont;
    [_scrollView addSubview:userAddress];
    _userAddress = userAddress;
    
    UITextView *textView = [[UITextView alloc]init];
    textView.frame = CGRectMake(userAddress.left, userAddress.bottom+10, userAddress.width, 195);
//    textView.backgroundColor = kCOLOR_HEX(0xe6ebf5);
    textView.font = [UIFont systemFontOfSize:16];
    [_scrollView addSubview:textView];
    _contentText = textView;
    
    UILabel *placeHolderLabel = [[UILabel alloc] init];
    placeHolderLabel.text = @"请点击填写活动内容";
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.textColor = kCOLOR_HEX(0x8a8a99);
    [placeHolderLabel sizeToFit];
    [textView addSubview:placeHolderLabel];
    
    // same font
    textView.font = [UIFont systemFontOfSize:14];
    placeHolderLabel.font = [UIFont systemFontOfSize:16];
    [textView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
    
    YYLabel *textLabel1 = [YYLabel new];
    textLabel1.frame = CGRectMake(10, _contentText.bottom, 70, 50);
    textLabel1.text = @"上传图片";
    textLabel1.font = _textFont;
    [_scrollView addSubview:textLabel1];
    
    YYLabel *textLabel2 = [YYLabel new];
    textLabel2.frame = CGRectMake(textLabel1.right, _contentText.bottom, kWindowW - textLabel1.right - 10, 50);
    textLabel2.text = @"(最多可以上传9张图片)";
    textLabel2.font = _textFont;
    textLabel2.textColor = _grayColor;
    [_scrollView addSubview:textLabel2];
    
    
    for (int i = 0; i < 6; i++) {
        CALayer *line = [CALayer layer];
        line.backgroundColor = kRGBColor(150, 150, 150).CGColor;
        line.frame = CGRectMake(0, (i + 1) * 49.5 , kWindowW, 0.5);
        [_scrollView.layer addSublayer:line];
    }
    CALayer *line2= [CALayer layer];
    line2.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line2.frame = CGRectMake(_startRightTime.left, _tfName.bottom , 1, 50);
    [_scrollView.layer addSublayer:line2];
    
    CALayer *line3 = [CALayer layer];
    line3.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line3.frame = CGRectMake(_stopRightTime.left, _startRightTime.bottom , 1, 50);
    [_scrollView.layer addSublayer:line3];
    
    CALayer *line4 = [CALayer layer];
    line4.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line4.frame = CGRectMake(locationBtn.left, _peopleNum.bottom - 1, 1, 50);
    [_scrollView.layer addSublayer:line4];
    
    CALayer *line5 = [CALayer layer];
    line5.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line5.frame = CGRectMake(0, _contentText.bottom - 0.5 , kWindowW, 0.5);
    [_scrollView.layer addSublayer:line5];
    
    _minY = textLabel2.bottom;
    
    [self setConfigSelImgBtn:_minY];
    
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    submitBtn.frame = CGRectMake(15, _minY + kWindowW/3.0 + 20 , kWindowW - 30, 40);
    submitBtn.backgroundColor = [UIColor redColor];
    [submitBtn setTitle:@"上传活动" forState:UIControlStateNormal];
    submitBtn.layer.cornerRadius = 5;
    
    [submitBtn addTarget:self action:@selector(submitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:submitBtn];
    _submitBtn = submitBtn;
    
    _scrollView.contentSize = CGSizeMake(kWindowW, _minY + kWindowW/3.0 + 80);
    
}

- (void)setConfigSelImgBtn:(CGFloat)minY
{
    CGFloat width = kWindowW/3.0;
    _selBtnTag = 0;
    
    for (int i = 0; i < 9; i++) {
        JSelImgView *btn = [[JSelImgView alloc]initWithFrame:CGRectMake(( i % 3 ) * width, minY + 10 + (i / 3) * width, width, width) tag:i];
        [self.scrollView addSubview:btn];
        btn.tag = i;
        btn.isBigImg = NO;
        [self.selImgBtnArr addObject:btn];
        
        if (i > 0) {
            btn.hidden = YES;
        }
        
        btn.delImgBtnBlock = ^(NSInteger tag) {
            
            JSelImgView *view = self.selImgBtnArr[i];
            if (view.isBigImg == NO) {
                return ;
            }
            
            [self.selImgArr removeObjectAtIndex:tag];
            
            for (int i = 0; i< self.selImgArr.count; i++) {
                JSelImgView *btn = self.selImgBtnArr[i];
                [btn.imgbtn setImage:self.selImgArr[i] forState:UIControlStateNormal];
            }
            
            _selImgNum -= 1;
            
            if (_selImgNum == 8)
            {
                JSelImgView *btn = self.selImgBtnArr[_selImgNum];
                [btn.imgbtn setImage:[UIImage imageNamed:@"gophone_icon"] forState:UIControlStateNormal];
                btn.isBigImg = NO;
            }
            else  {
                JSelImgView *btn = self.selImgBtnArr[_selImgNum+1];
                btn.isBigImg = NO;
                btn.hidden = YES;
                
                JSelImgView *btn2 = self.selImgBtnArr[_selImgNum];
                [btn2.imgbtn setImage:[UIImage imageNamed:@"gophone_icon"] forState:UIControlStateNormal];
                btn2.isBigImg = NO;
            }
            
            _scrollView.contentSize = CGSizeMake(kWindowW, (self.selImgArr.count / 3 + 1) * (kWindowW/3.0) + 80 + _minY);
        };
        
        btn.addImgBtnBlock = ^(NSInteger tag) {
            JSelImgView *view = self.selImgBtnArr[tag];
            
            if (view.isBigImg == NO) {
                [self setImagePickerBtn:tag];
            }
            else
            {
                NSLog(@"放大");
            }
            
        };
    }
}

- (void)selTimeClick
{
    __weak typeof(self) weakSelf = self;
    _startLeftTime.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//        NSLog(@"选择开始时间，年月日");
        [weakSelf.view endEditing:YES];
        [weakSelf selelctTimeAction:3];
    };
    
    _startRightTime.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//        NSLog(@"选择开始时间，时分");
        [weakSelf.view endEditing:YES];
        [weakSelf selelctTimeAction:5];
    };
    
    _stopLeftTime.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//        NSLog(@"选择结束时间，年月日");
        [weakSelf.view endEditing:YES];
        [weakSelf selelctTimeAction:3];

    };
    
    _stopRightTime.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//        NSLog(@"选择结束时间，时分");
        [weakSelf.view endEditing:YES];
        [weakSelf selelctTimeAction:5];
    };
}

- (void)peopleNumTextField:(UITextField *)textField
{
    NSLog(@"textField====%@",textField.text);
    if (textField.text.length > 5) {
        textField.text = _peopleStr;
        return;
    }
    NSString *str = textField.text;
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(str.length > 0) {
        textField.text = _peopleStr;
        return;
    }
    
    _peopleStr = textField.text;

}


- (void)locationBtnClick:(UIButton *)btn
{
//    NSLog(@"定位用户位置，并显示省市区位置");
    NSDictionary *dic = [UserDefaultsDelegate objectForKey:UserLocationCity];
    if (dic[@"cityLocation"]) {
        NSString *str = dic[@"cityLocation"];
        _userLocation.text = str;
    }
    
}

- (void)setImagePickerBtn:(NSInteger)tag
{
    _selBtnTag = tag;
        
    UIAlertController *sheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *xiangji = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self xiangjiMethod:tag];
    }];
    
    UIAlertAction *xiangce = [UIAlertAction actionWithTitle:@"手机相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self xiangceMethod:tag];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [sheetController addAction:xiangji];
    [sheetController addAction:xiangce];
    [sheetController addAction:cancel];
    
    [self presentViewController:sheetController animated:YES completion:nil];
    
}

- (void)xiangjiMethod:(NSInteger)tag {
    if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]){
        
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        //        self.imagePC = imagePickerController;
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;//no
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePickerController.showsCameraControls  = YES;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }else{
        
        //        jHUDNormal(@"您的设备不支持照相功能!")
    }
}

//#pragma mark  imagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    //
    CGFloat width = kWindowW/3.0;
    NSData *imgData = [self resetSizeOfImageData:image maxSize:width];
    image = [UIImage imageWithData:imgData];
    
    JSelImgView *view = self.selImgBtnArr[_selBtnTag];
    [view.imgbtn setImage:image forState:UIControlStateNormal];
    view.isBigImg = YES;
    [self.selImgArr addObject:image];
    
    _selImgNum = view.tag +1;
    
    if (_selBtnTag < 9) {
        JSelImgView *view2 = self.selImgBtnArr[_selBtnTag+1];
        view2.hidden = NO;
    }
    
    _scrollView.contentSize = CGSizeMake(kWindowW, (self.selImgArr.count / 3 + 1) * (kWindowW/3.0) + 80 + _minY);
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)xiangceMethod:(NSInteger)tag {
    QBImagePickerController *imagePickerController =[QBImagePickerController new];
    imagePickerController.delegate = self;
    imagePickerController.allowsMultipleSelection = YES;
    imagePickerController.mediaType = QBImagePickerMediaTypeImage;//图片
    
    imagePickerController.showsNumberOfSelectedAssets = YES;//在界面下方显示已经选择图像的数量
    
    imagePickerController.maximumNumberOfSelection = 9 - _selImgNum;
    imagePickerController.prompt = [NSString stringWithFormat:@"请选择图片，最多%ld张",9 - _selImgNum];
    [self presentViewController:imagePickerController animated:YES completion:nil];
}

//1.获取到图像
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets {
    
    for (int i = 0; i< assets.count; i++) {
        PHAsset * asset = assets[i];
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        // 同步获得图片, 只会返回1张图片
        options.synchronous = YES;
        
        // 是否要原图
        CGSize size = /* DISABLES CODE */ (YES) ? CGSizeMake(asset.pixelWidth, asset.pixelHeight) : CGSizeZero;
        
        // 从asset中获得图片
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            
            CGFloat width = kWindowW/3.0;
            NSData *imgData = [self resetSizeOfImageData:result maxSize:width];
            result = [UIImage imageWithData:imgData];
            
            [self.selImgArr addObject:result];
            
            JSelImgView *view = self.selImgBtnArr[_selBtnTag+i];
            [view.imgbtn setImage:result forState:UIControlStateNormal];
            view.isBigImg = YES;
            view.hidden = NO;
            _selImgNum += 1;
            
            if (_selImgNum < 9) {
                JSelImgView *view = self.selImgBtnArr[_selImgNum];
                view.isBigImg = NO;
                view.hidden = NO;
            }
            
        }];
        
        
    }
    _scrollView.contentSize = CGSizeMake(kWindowW, (self.selImgArr.count / 3 + 1) * (kWindowW/3.0) + 80 + _minY);
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
//2.取消获取图像
- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController {
    //do something
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)submitBtnClick:(UIButton *)btn
{
    if ([self requestBody] == NO) return;
    
    btn.enabled = NO;
    
    [SVProgressHUD showWithStatus:@"创建中。。。"];
    
    NSString *url = @"ActivityMine/AddActivity";
    [JNetManager subBeautifulSocialHubRequedtUrl:url
                                          dicPar:self.paramerDic
                                         success:^(id response) {
                                             btn.enabled = YES;
                                             if ([response integerValue] == 1) {
                                                 [SVProgressHUD showSuccessWithStatus:@"创建成功,信息需要审核后才能发布"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                             }
                                         } failure:^(NSString *err) {
                                             btn.enabled = YES;
                                             [SVProgressHUD showErrorWithStatus:err];
                                         }];
    
}

- (BOOL)requestBody {
    if (_tfName.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"活动主题不能为空"];
        return NO;
    }
    if ([_startLeftTime.text isEqualToString:@"年-月-日"]) {
        [SVProgressHUD showErrorWithStatus:@"开始时间不能为空"];
        return NO;
    }
    if ([_stopLeftTime.text isEqualToString:@"年-月-日"]) {
        [SVProgressHUD showErrorWithStatus:@"结束时间不能为空"];
        return NO;
    }
    
    if (_peopleStr.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"人数限制不能为空"];
        return NO;
    }
    if (_userLocation.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"省市区地址不能为空"];
        return NO;
    }
    if (_userAddress.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"详细地址不能为空"];
        return NO;
    }
    
    if (_contentText.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"活动内容不能为空"];
        return NO;
    }
    
    if (self.selImgArr.count == 0) {
        [SVProgressHUD showErrorWithStatus:@"请至少上传一张图片"];
        return NO;
    }
    
    NSString *start = [NSString stringWithFormat:@"%@ %@",_startLeftTime.text,_startRightTime.text];
    NSString *stop = [NSString stringWithFormat:@"%@ %@",_stopLeftTime.text,_stopRightTime.text];
    
    
    NSInteger num = [self compareDate:start withDate:stop];
    if (num < 1 ) {
        [SVProgressHUD showErrorWithStatus:@"开始时间不能小于或等于结束时间"];
        return NO;
    }
    
    [self.paramerDic setObject:_tfName.text forKey:@"Topic"];
    [self.paramerDic setObject:start forKey:@"StartTime"];
    [self.paramerDic setObject:stop forKey:@"EndTime"];
    [self.paramerDic setObject:_peopleStr forKey:@"LimitPeoples"];
    [self.paramerDic setObject:_userLocation.text forKey:@"Address"];
    [self.paramerDic setObject:_userAddress.text forKey:@"DetailAddress"];
    [self.paramerDic setObject:_contentText.text forKey:@"Content"];
    [self.paramerDic setObject:@([GLOBALDATA userInfoModel].CustomerId) forKey:@"CreatorId"];
    
    NSMutableArray *picArr = [NSMutableArray array];
    
    for (UIImage *img in self.selImgArr) {
        NSData *data = UIImageJPEGRepresentation(img, 0.2f);
        NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        
        
        JPicModel *picModel = [JPicModel new];
        picModel.MimeType = @"image/jpeg";
        picModel.TitleAttribute = encodedImageStr;
        picModel.SeoFilename = @"";
        [picArr addObject:picModel.mj_keyValues];
        
    }
    
    [self.paramerDic setObject:picArr forKey:@"PIC"];

    return YES;
}


- (void)leftButtonClick:(UIButton *)leftButton
{
    [super leftButtonClick:leftButton];
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 获取当前的日期
 日期格式为:yyyy-MM-dd HH-mm
 */
- (NSString *)getCurrentDate
{
    // 设置 日期的格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    // 获取 当前系统时间
    NSString *dataTime = [formatter stringFromDate:[NSDate date]];
    return dataTime;
}

/**
 比较两个日期的大小
 日期格式为:yyyy-MM-dd HH-mm
 */
-(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{
    int ci;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *dt1 = [[NSDate alloc] init];
    NSDate *dt2 = [[NSDate alloc] init];
    dt1 = [df dateFromString:date01];
    dt2 = [df dateFromString:date02];
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result)
    {
            //date02比date01大
        case NSOrderedAscending: ci=1; break;
            //date02比date01小
        case NSOrderedDescending: ci=-1; break;
            //date02=date01
        case NSOrderedSame: ci=0; break;
        default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
    }
    return ci;
}

// 压缩图片
- (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize
{
    //先调整分辨率
    CGSize newSize = CGSizeMake(source_image.size.width, source_image.size.height);
    
    CGFloat tempHeight = newSize.height / 1024;
    CGFloat tempWidth = newSize.width / 1024;
    
    if (tempWidth > 1.0 && tempWidth > tempHeight) {
        newSize = CGSizeMake(source_image.size.width / tempWidth, source_image.size.height / tempWidth);
    }
    else if (tempHeight > 1.0 && tempWidth < tempHeight){
        newSize = CGSizeMake(source_image.size.width / tempHeight, source_image.size.height / tempHeight);
    }
    
    UIGraphicsBeginImageContext(newSize);
    [source_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //调整大小
    NSData *imageData;
    if (UIImagePNGRepresentation(newImage)) {
        imageData = UIImagePNGRepresentation(newImage);
    }else{
        imageData = UIImageJPEGRepresentation(newImage, 0.5);
    }
    
    NSUInteger sizeOrigin = [imageData length];
    NSUInteger sizeOriginKB = sizeOrigin / 1024;
    if (sizeOriginKB > maxSize) {
        NSData *finallImageData = UIImageJPEGRepresentation(newImage,0.50);
        return finallImageData;
    }
    
    return imageData;
}


- (void)selelctTimeAction:(NSInteger)selType {
    XHDateStyle dateStyle;
    NSString *format;
    switch (selType) {
        case 1:
            dateStyle = DateStyleShowYearMonthDayHourMinute;
            format = @"yyyy-MM-dd HH:mm";
            break;
        case 2:
            dateStyle = DateStyleShowMonthDayHourMinute;
            format = @"MM-dd HH:mm";
            break;
        case 3:
            dateStyle = DateStyleShowYearMonthDay;
            format = @"yyyy-MM-dd";
            break;
        case 4:
            dateStyle = DateStyleShowMonthDay;
            format = @"MM-dd";
            break;
        case 5:
            dateStyle = DateStyleShowHourMinute;
            format = @"HH:mm";
            break;
            
        default:
            dateStyle = DateStyleShowYearMonthDayHourMinute;
            format = @"yyyy-MM-dd HH:mm";
            break;
    }
    
    
    XHDatePickerView *datepicker = [[XHDatePickerView alloc] initWithCurrentDate:[NSDate date] CompleteBlock:^(NSDate *startDate, NSDate *endDate) {
//        NSLog(@"\n开始时间： %@，结束时间：%@",startDate,endDate);
        if (selType == 3) {
            if (startDate) {
                self.startLeftTime.text = [startDate stringWithFormat:format];
                self.startLeftTime.textColor = [UIColor blackColor];
            }
            
            if (endDate) {
                self.stopLeftTime.text = [endDate stringWithFormat:format];
                self.stopLeftTime.textColor = [UIColor blackColor];
            }
        }
        else if (selType == 5)
        {
            if (startDate) {
                self.startRightTime.text = [startDate stringWithFormat:format];
            }
            
            if (endDate) {
                self.stopRightTime.text = [endDate stringWithFormat:format];
            }
        }
        
        
    }];
    
    //    XHDatePickerView *datepicker = [[XHDatePickerView alloc] initWithCompleteBlock:^(NSDate *startDate,NSDate *endDate) {
    //        NSLog(@"\n开始时间： %@，结束时间：%@",startDate,endDate);
    //        self.startTimeText.text = [startDate stringWithFormat:@"yyyy-MM-dd HH:mm"];
    //        self.endtimeText.text = [endDate stringWithFormat:@"yyyy-MM-dd HH:mm"];
    //    }];
    datepicker.datePickerStyle = dateStyle;
    datepicker.dateType = DateTypeStartDate;
    datepicker.minLimitDate = [NSDate date:[self getCurrentDate] WithFormat:@"yyyy-MM-dd HH:mm"];
    datepicker.maxLimitDate = [NSDate date:@"2020-5-20 20:20" WithFormat:@"yyyy-MM-dd HH:mm"];
    [datepicker show];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [SVProgressHUD dismiss];
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
